package com.ys.erp.stage_mall_service.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ys.erp.common.enums.RedisKey;
import com.ys.erp.entity.data.bean.MallHomeGoods;
import com.ys.erp.entity.data.bean.MallHomeGroupArea;
import com.ys.erp.entity.data.bean.MallHomeGroupPutUpArea;
import com.ys.erp.entity.service.*;
import com.ys.erp.entity.service.impl.IOrderServiceImpl;
import com.ys.erp.stage_mall_service.bean.condition.HomeGroupAreaCondition;
import com.ys.erp.stage_mall_service.service.HomeGroupAreaService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author minamiyama
 * @date 2025/6/6 13:51
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class ImportGoodsRefreshTask {
    private final IMallHomeGroupAreaService iMallHomeGroupAreaService;
    private final IMallHomeGroupPutUpAreaService iMallHomeGroupPutUpAreaService;
    private final IMallHomeGoodsService iMallHomeGoodsService;
    private final RedissonClient redisson;
    private final HomeGroupAreaService homeGroupAreaService;

    @Scheduled(cron = "0 0 0 * * ?")
    public void groupAreaImportGoodsRefresh() {
        RLock lock = redisson.getLock(RedisKey.Enums.IMPORT_GOODS_REFRESH.getKey());
        boolean b = lock.tryLock();
        if (!b) {
            return;
        }
        try {
            List<MallHomeGroupArea> groupAreas = iMallHomeGroupAreaService.list(new QueryWrapper<MallHomeGroupArea>().lambda().eq(MallHomeGroupArea::getId,68));
            for (MallHomeGroupArea groupArea : groupAreas) {
                LambdaQueryWrapper<MallHomeGoods> mallHomeGoodsQw = new LambdaQueryWrapper<>();
                mallHomeGoodsQw.in(MallHomeGoods::getHomeGroupNo, groupArea.getNo());
                iMallHomeGoodsService.remove(mallHomeGoodsQw);
                LambdaQueryWrapper<MallHomeGroupPutUpArea> putUpAreaQw = new LambdaQueryWrapper<>();
                putUpAreaQw.eq(MallHomeGroupPutUpArea::getHomeGrepAreaNo, groupArea.getNo());
                List<MallHomeGroupPutUpArea> putUpAreas = iMallHomeGroupPutUpAreaService.list(putUpAreaQw);
                List<MallHomeGoods> mallHomeGoods = new ArrayList<>();
                if (CollectionUtils.isNotEmpty(putUpAreas)) {
                    List<MallHomeGroupPutUpArea> importPutArea = putUpAreas.stream()
                            .filter(it -> it.getGoodsSettingType()
                                    .equals(MallHomeGroupPutUpArea.Enums.IMPORT_RULE.toString()))
                            .toList();
                    List<String> putUpAreaNo = importPutArea.stream().map(MallHomeGroupPutUpArea::getNo).toList();
                   /* LambdaQueryWrapper<MallHomeGoods> mallHomeGoodsQw = new LambdaQueryWrapper<>();
                    mallHomeGoodsQw.in(MallHomeGoods::getPutUpAreaNo, putUpAreaNo);
                    iMallHomeGoodsService.remove(mallHomeGoodsQw);*/

                    for (MallHomeGroupPutUpArea putUpArea : importPutArea) {
                        // 如果规则类型为按新上线商品和销量排序，获取符合条件的商品列表
                        if (putUpArea.getImportRule().getType().equals(MallHomeGroupPutUpArea.Enums.NEW_LINE_SALE_NUM.toString())) {
                            List<MallHomeGoods> goodsList = homeGroupAreaService
                                    .getGoodsByNewLineDayAndSaleNum(putUpArea.getImportRule().getDay(), putUpArea.getImportRule().getNum());
                            // 为每个商品设置分组区域ID，并添加到商品列表中
                            goodsList.forEach(goods -> {
                                goods.setPutUpAreaNo(putUpArea.getNo());
                                goods.setHomeGroupNo(groupArea.getNo());
                            });
                            mallHomeGoods.addAll(goodsList);
                        }
                        // 如果规则类型为按最近N天销量排序，获取符合条件的商品列表
                        if (putUpArea.getImportRule().getType().equals(MallHomeGroupPutUpArea.Enums.N_DAY_SALE_NUM.toString())) {
                            List<MallHomeGoods> goodsList = homeGroupAreaService
                                    .getGoodsByNDayAndSaleNum(putUpArea.getImportRule().getDay(), putUpArea.getImportRule().getNum());
                            // 为每个商品设置分组区域ID，并添加到商品列表中
                            goodsList.forEach(goods -> {
                                goods.setPutUpAreaNo(putUpArea.getNo());
                                goods.setHomeGroupNo(groupArea.getNo());
                            });
                            mallHomeGoods.addAll(goodsList);
                        }
                    }
                }
                if (groupArea.getGoodsSettingType().equals(MallHomeGroupArea.Enums.MANUAL.toString())) {
                    iMallHomeGoodsService.saveBatch(mallHomeGoods);
                    continue;
                }

                // 自动引入商品时，根据不同的引入规则处理
                MallHomeGroupArea.ImportRuleDTO importRule = groupArea.getImportRule();
                if (importRule.getType().equals(MallHomeGroupArea.Enums.NEW_LINE.toString())) {
                    // 如果是按新品引入规则，获取符合条件的新品列表，并设置团购区域ID
                    List<MallHomeGoods> goodsList = homeGroupAreaService.getGoodsByNewLineDay(importRule.getDay());
                    goodsList.forEach(goods -> goods.setHomeGroupNo(groupArea.getNo()));
                    mallHomeGoods.addAll(goodsList);
                }
                iMallHomeGoodsService.saveBatch(mallHomeGoods);

            }
        } finally {
            lock.unlock();
        }


    }
}
